package org.md.budzik.gui;

import android.app.Activity;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.md.budzik.*;
import org.md.budzik.problems.Problem;

/**
 * Aktywność wyświetlana w momencie uruchomienia
 * "dyskretnego" budzenia. Nie daje spokoju aż
 * do rozwiązania zadania.
 * @author Saephir
 *
 */
public class MDProblemActivity extends Activity {
	
	private static MDBudzikActivity settingsActivity;
	
	private static MediaPlayer mp;
	
	private Button solved;
	private TextView problemView;
	private EditText solution;
	
	private String problemSolution;
	private String problemContent;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zadanie);
        
        // Kończymy działanie aktywności ustawiania budzika
        if(settingsActivity != null)
        	settingsActivity.finish();
        
        if(mp == null)
        	mp = MediaPlayer.create(this, R.raw.dzwonek);
        
        // ustaw status OFF w preferencjach
        alarmOff();
        
        solution = (EditText)findViewById(R.id.editText1);
        
        solved = (Button)findViewById(R.id.button1);
        solved.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// Jeżeli rozwiązanie jest dobre, to kończymy ten cyrk
				if(problemSolution.equals(solution.getText().toString())) {
					mp.pause();
					mp.seekTo(0);
					alarmOff();
					finish();
				}
				else {
					Toast.makeText(MDProblemActivity.this, "Błędne rozwiązanie!", Toast.LENGTH_LONG).show();
				}
			}
        	
        });
        
        problemView = (TextView)findViewById(R.id.textView2);
        
        if(!isMusicPlaying())
        {
	        // Wybieramy losowy problem
	        try {
				Problem problem = Problem.getRandomProblem(getApplicationContext());
				problemSolution = problem.getResult();
				problemContent = "\n"+problem.getText()+"\n"+problem.getVariableString()+"\n";
	        	problemView.setText(problemContent);

			    mp.setLooping(true);
			    mp.start();
			        
			} catch (Exception e) {
				e.printStackTrace();
			}
        }
        else
        {
        	SharedPreferences prefs = getSharedPreferences(MDBudzikActivity.PREFS_NAME, 0);
        	solution.setText(prefs.getString("answer", ""));
        	problemContent = prefs.getString("content", "");
        	problemView.setText(problemContent);
        	problemSolution = prefs.getString("solution", "");
        }

    }
    
    /**
     * Wyłącz alarm w preferencjach.
     */
    private void alarmOff() {
    	SharedPreferences settings = getSharedPreferences(MDBudzikActivity.PREFS_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean("switchOn", false);
		editor.commit();
    }
    
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
    	super.onSaveInstanceState(outState);
    	saveState();
    }
    
    @Override
    protected void onStop() {
    	super.onStop();
    	saveState();
    }
    
    /**
     * Zapisujemy stan aktywności
     */
    private void saveState() {
    	SharedPreferences prefs = getSharedPreferences(MDBudzikActivity.PREFS_NAME, 0);
    	SharedPreferences.Editor editor = prefs.edit();
    	editor.putString("answer", solution.getText().toString());
    	editor.putString("solution", problemSolution);
    	editor.putString("content", problemContent);
    	editor.commit();
    }
    
    /**
     * Pobierz instancję aktywności do ustawiania budzika.
     * @return
     */
	public static MDBudzikActivity getSettingsActivity() {
		return settingsActivity;
	}

	/**
	 * Ustaw nową instancję aktywności do ustawiania budzika.
	 * @param settingsActivity
	 */
	public static void setSettingsActivity(MDBudzikActivity settingsActivity) {
		MDProblemActivity.settingsActivity = settingsActivity;
	}
	
	/**
	 * Czy teraz jest odtwarzana muzyka?
	 * @return
	 */
	public static boolean isMusicPlaying() {
		if(mp == null)
			return false;
		else
			return mp.isPlaying();
	}
	
}